using System.Threading;
using System.Threading.Tasks;
using InventoryService.Domain.Event;
using MediatR;
using Microsoft.Extensions.Logging;

namespace InventoryService.Infrastructure.PubSub
{
    /// <summary>
    /// 
    /// </summary>
    public class InventoryCreatedNotificationHandler :
        INotificationHandler<InventoryCreatedEvent>
    {
        private readonly ILogger<InventoryCreatedNotificationHandler> _logger;
        private readonly DaprInventoryCreatedPublisher _daprPublisher;


        public InventoryCreatedNotificationHandler(ILogger<InventoryCreatedNotificationHandler> logger,
            DaprInventoryCreatedPublisher daprPublisher)
        {
            _logger = logger;
            _daprPublisher = daprPublisher;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(InventoryCreatedEvent notification, CancellationToken cancellationToken)
        {
            await _daprPublisher.Publish(notification, cancellationToken);
            _logger.LogInformation(
                $"[{nameof(InventoryCreatedNotificationHandler)}]: Published notification - {notification}");
        }
    }
}